
import {
    getOrgList,
    addOrg,
    updateOrg,
    deleteOrg,
    isExistOrgCode,
    getUserListByOrgCode, GetParentOptionItemOrgList
} from "@/api/suborg";
import { deepClone } from "@/utils";
const defaultorg = {
    org_id: "",
    org_code: "",
    org_name: "",
    parent_code: '',
    org_type: "",
    org_owner: "",
    owner_call: "",
    owner_phone: "",
    org_adress: "",
    org_remark: "",
    Creater: "",
    CreateTime: "",
    Modifyer: "",
    ModifyTime: "",
    IsDelete: "",
    AccountLimit: 0
};
export default {
    filters: {
        typeFilter(type) {
            const typeMap = {
                "": "全部",
                1: "学校",
                2: "公司",
                3: "默认",
                4: "其他"
            };
            return typeMap[type];
        }
    },
    data() {
        var checkPhone = (rule, value, callback) => {
            var myreg = /^[1][0-9]{10}$/;
            if (value === "") {
                callback(new Error("请输入手机号"));
            } else if (!myreg.test(value)) {
                callback(new Error("手机号格式不正确"));
            } else {
                callback();
            }
        };
        var checkorg_code = (rule, value, callback) => {
            var id = this.org.org_id;
            if (value === "") {
                callback(new Error("请输入组织编码"));
            } else {
                this.isExistOrg_Code(id, value).then(res => {
                    if (res) callback(new Error("组织编码已经存在，请重新输入"));
                    else callback();
                });
            }
        };
        return {
            typeOptions: ["", 1, 2, 3, 4],
            OrgViewModel: {
                page: 1,
                pageSize: 10,
                total: 0,
                org_type: "",
                org_code: "",
                org_name: "",
                org_owner: "",
                OrderBy: "CreateTime", //排序字段
                IsAsc: "true" //是否升序
            },
            dialogVisible: false,
            dialogVisibleshow: false,
            tempMark: false,
            dialogType: "",
            org: Object.assign({}, defaultorg),
            orgList: [],
            loading: true,
            teacherList: [],
            username: "",
            rules: {
                org_type: [
                    { required: true, message: "请选择机构类别", trigger: "change" }
                ],
                org_name: [
                    { required: true, message: "请输入组织名", trigger: "blur" },
                    {
                        min: 1,
                        max: 50,
                        message: "长度在 1 到 50 个字符",
                        trigger: "blur"
                    }
                ],
                org_code: [
                    { required: true, message: "请输入组织编码", trigger: "blur" }
                    // { validator: checkorg_code, trigger: "blur" },
                ],
                org_owner: [
                    { required: true, message: "请输入管理员", trigger: "blur" }
                ],
                owner_phone: [
                    { required: true, message: "请输入手机号", trigger: "blur" }
                ]
            },
            parentOrgList: []
        };
    },

    computed: {
        // 计算属性，只要相关联的值没有改变，直接返回，不再进行计算，与方法区别，缓存
    },
    created() {
        // 实例创建完成后执行的代码
        this.getOrgList(this.OrgViewModel);
        this.GetParentOptionItemOrgList()
    },
    methods: {
        async GetParentOptionItemOrgList() {
            var res = await GetParentOptionItemOrgList();
            if (res && res.code == 200) {
                this.parentOrgList = res.data.OrgItems
                this.parentOrgList.unshift(
                    {
                        org_code: '',
                        org_name: '无'
                    }
                )
            }
        },
        async getOrgList(OrgViewModel) {
            const res = await getOrgList(OrgViewModel);
            if (res.data) {
                this.OrgViewModel.total = res.data.TotalCount;
            }
            this.orgList = res.data.OrgItems;
            this.loading = false;
        },
        async isExistOrg_Code(id, org_ode) {
            const res = await isExistOrgCode(id, org_ode);
            this.tempMark = res.data;
            return res.data;
        },
        handleAddRole() {
            this.org = Object.assign({}, defaultorg);
            this.dialogType = "new";
            this.dialogVisible = true;
            this.$nextTick(() => {
                this.$refs["org"].clearValidate(); //清空上次的错误验证提示
            });
        },
        handleEdit(scope) {
            this.dialogType = "edit";
            this.dialogVisible = true;
            this.org = deepClone(scope.row);
            this.$nextTick(() => {
                this.$refs["org"].clearValidate(); //清空上次的错误验证提示
            });
        },
        handleShow(scope) {
            this.dialogVisibleshow = true;
            this.org = deepClone(scope.row);
            this.$nextTick(() => {
                this.username = ""; //清空上次的错误验证提示
                this.getusersByOrgCode(scope.row);
            });
        },
        handleDelete({ $index, row }) {
            this.$confirm("确认删除组织?", "提示", {
                confirmButtonText: "确认",
                cancelButtonText: "取消",
                type: "warning"
            })
                .then(async () => {
                    await deleteOrg(row.org_id);
                    this.$notify({
                        message: `删除成功`,
                        type: "success",
                        title: "成功",
                        duration: 3000
                    });
                    this.getOrgList(this.OrgViewModel);
                })
                .catch(err => {
                    console.error(err);
                });
        },
        async getusersByOrgCode(row) {
            const res = await getUserListByOrgCode(row.org_code, this.username);
            if (res.data) {
            }
            this.teacherList = res.data.UserItems;
            this.loading = false;
        },
        async confirmRole(formName) {
            //如果把验证失败弹出提示代码放在validate里面则不会弹出错误提示，直接走下面的更新操作代码
            var v;
            this.$refs[formName].validate(valid => {
                v = valid;
            });
            if (!v) {
                return false;
            }
            const isEdit = this.dialogType === "edit";

            if (isEdit) {
                await updateOrg(this.org);
            } else {
                await addOrg(this.org);
                this.OrgViewModel.page = 1;
            }
            this.org = Object.assign({}, defaultorg);
            this.getOrgList(this.OrgViewModel);
            this.GetParentOptionItemOrgList()
            this.dialogVisible = false;
            this.$notify({
                message: `操作成功`,
                type: "success",
                title: "成功",
                duration: 3000
            });
        },
        queryUser() {
            this.OrgViewModel.page = 1;
            this.getOrgList(this.OrgViewModel);
        },
        queryUser1() {
            this.getusersByOrgCode(this.org);
        },
        refreshUser() {
            this.OrgViewModel.org_code = "";
            this.OrgViewModel.org_name = "";
            this.OrgViewModel.org_type = "";
            this.OrgViewModel.org_owner = "";
            this.OrgViewModel.page = 1;
            this.getOrgList(this.OrgViewModel);
        },
        sortChange(data) {
            const { prop, order } = data;
            this.OrgViewModel.OrderBy = prop;
            if (prop == null || prop == "") this.OrgViewModel.OrderBy = "CreateTime";
            if (order === "ascending") {
                this.OrgViewModel.IsAsc = true;
            } else {
                this.OrgViewModel.IsAsc = false;
            }
            this.queryUser();
        },
        lastPage(val) {
            this.OrgViewModel.page = val;
        },
        nextPage(val) {
            this.OrgViewModel.page = val;
        },
        handleSizeChange(val) {
            this.OrgViewModel.pageSize = val;
            this.getOrgList(this.OrgViewModel);
        },
        handleCurrentChange(val) {
            this.OrgViewModel.page = val;
            this.getOrgList(this.OrgViewModel);
        }
    }
};